/*
 * @Author: shijianxin
 * @Date: 2022-04-21 16:32:50
 * @Description: 电影路由
 */

import Express from "express";
import { SearchCondition } from "../entities/SearchCondition";
import { MovieService } from "../services/MovieService";
import { ResponseHelper } from "./ResponseHelper";

const router = Express.Router();

router.get("/:id", async (req, res) => {
  try {
    const movieId = req.params.id;
    const movie = await MovieService.findById(movieId);
    ResponseHelper.sendData(movie, res);
  } catch (error) {
    ResponseHelper.sendData(null, res);
  }
});

router.get("/", async (req, res) => {
  const result = await MovieService.find(
    req.query as unknown as SearchCondition
  );
  ResponseHelper.sendPageData(result, res);
});

router.post("/", async (req, res) => {
  const result = await MovieService.add(req.body);
  if (Array.isArray(result)) {
    ResponseHelper.sendData(result, res);
  } else {
    ResponseHelper.sendData(result, res);
  }
});

router.put("/:id", async (req, res) => {
  try {
    const result = await MovieService.edit(req.params.id, req.body);
    ResponseHelper.sendData(result, res);
  } catch (error) {
    ResponseHelper.sendError("id错误", res);
  }
});

router.delete("/:id", async (req, res) => {
  try {
    await MovieService.delete(req.params.id);
    ResponseHelper.sendData(true, res);
  } catch (error) {
    ResponseHelper.sendError("id错误", res);
  }
});

export default router;
